package cn.com.blueInfo.bpm.number.service.impl;

import cn.com.blueInfo.bpm.number.convert.NumberLogConvert;
import cn.com.blueInfo.bpm.number.entity.NumberLog;
import cn.com.blueInfo.bpm.number.repository.NumberLogRepository;
import cn.com.blueInfo.bpm.number.request.NumberLogDTO;
import cn.com.blueInfo.bpm.number.response.NumberLogVO;
import cn.com.blueInfo.bpm.number.service.NumberLogService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 编号规则日志表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberLogServiceImpl implements NumberLogService {

    @Autowired
    private NumberLogRepository numberLogRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private NumberLogConvert numberLogConvert;

    /**
     * 新增编号规则日志
     * @param numberLogDTO 编号规则日志参数
     */
    @Override
    public ResultInfo<Integer> addNumberLog(NumberLogDTO numberLogDTO) {
        NumberLog numberLog = numberLogConvert.dtoToEntity(numberLogDTO);
        numberLog.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberLogRepository.createNumberLog(numberLog);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则日志成功新增 {} 条").build();
    }

    /**
     * 批量新增编号规则日志
     * @param numberLogDTOList 编号规则日志参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddNumberLog(List<NumberLogDTO> numberLogDTOList) {
        List<NumberLog> numberLogList = numberLogConvert.dtoToEntity(numberLogDTOList);
        for (NumberLog numberLog : numberLogList) numberLog.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberLogRepository.batchCreateNumberLog(numberLogList);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则日志成功新增 {} 条").build();
    }

    /**
     * 删除编号规则日志
     * @param numberLogId 编号规则日志ID
     */
    @Override
    public ResultInfo<Integer> removeNumberLog(Long numberLogId) {
        Integer removeSize = numberLogRepository.deleteNumberLog(numberLogId);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则日志成功删除 {} 条").build();
    }

    /**
     * 批量删除编号规则日志
     * @param numberLogIdList 编号规则日志ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveNumberLog(List<Long> numberLogIdList) {
        Integer removeSize = numberLogRepository.batchDeleteNumberLog(numberLogIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则日志成功删除 {} 条").build();
    }

    /**
     * 更新部分编号规则日志
     * @param numberLogUpdatePartialDTO 编号规则日志参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberLogPartial(NumberLogDTO.UpdatePartialDTO numberLogUpdatePartialDTO) {
        NumberLog numberLog = numberLogConvert.dtoToEntity(numberLogUpdatePartialDTO);
        Integer modifySize = numberLogRepository.updateNumberLogPartial(numberLog);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则日志成功更新 {} 条").build();
    }

    /**
     * 更新全部编号规则日志
     * @param numberLogDTO 编号规则日志参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberLog(NumberLogDTO numberLogDTO) {
        NumberLog numberLog = numberLogConvert.dtoToEntity(numberLogDTO);
        Integer modifySize = numberLogRepository.updateNumberLog(numberLog);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则日志成功更新 {} 条").build();
    }

    /**
     * 查询一条编号规则日志
     * @param numberLogId 编号规则日志ID
     */
    @Override
    public ResultInfo<NumberLogVO> queryNumberLogById(Long numberLogId) {
        NumberLog numberLog = numberLogRepository.getNumberLogById(numberLogId);
        NumberLogVO numberLogVO = numberLogConvert.entityToVO(numberLog);
        return ResultInfo.<NumberLogVO>builder().data(numberLogVO).message("编号规则日志查询成功").build();
    }

    /**
     * 分页查询编号规则日志
     * @param numberLogQueryDTO 编号规则日志参数
     */
    @Override
    public ResultInfo<Page<NumberLogVO>> queryNumberLogPage(NumberLogDTO.QueryDTO numberLogQueryDTO) {
        Page<NumberLog> numberLogPage = numberLogRepository.getNumberLogPage(numberLogQueryDTO);
        Page<NumberLogVO> resultData = numberLogConvert.convertPage(numberLogPage);
        return ResultInfo.<Page<NumberLogVO>>builder()
                .data(resultData)
                .message("编号规则日志分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 编号规则日志 数量
     * @param numberLogQueryDTO 编号规则日志参数
     */
    @Override
    public ResultInfo<Long> queryNumberLogCount(NumberLogDTO.QueryDTO numberLogQueryDTO) {
        Long resultCount = numberLogRepository.getNumberLogCount(numberLogQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("编号规则日志数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 编号规则日志
     * @param numberLogQueryDTO 编号规则日志参数
     */
    @Override
    public ResultInfo<List<NumberLogVO>> queryNumberLogList(NumberLogDTO.QueryDTO numberLogQueryDTO) {
        List<NumberLog> numberLogList = numberLogRepository.getNumberLogList(numberLogQueryDTO);
        List<NumberLogVO> resultData = numberLogConvert.entityListToVoList(numberLogList);
        return ResultInfo.<List<NumberLogVO>>builder()
                .data(resultData)
                .message("编号规则日志分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
